'use strict'

let PlayerComponent = require('../component/playerComponent')
let PackageComponent = require('../component/packageComponent')
let templateConfig = require('../../config/templateConfig')
let playerDao = require('../dao/playerDao')

//创建玩家信息
class CreateNewPlayerHandle {
  constructor() {}
  create(opts, cb){
    let characterInfo = templateConfig.total[opts.templateId]
    let bag = new PackageComponent({ //初始化背包
      packageType: 1
    })

    let player = new PlayerComponent({
      name: opts.name,
      templateId: opts.templateId,
      userId: opts.userId,
      bag: bag,
      equipments: {},
      basicInfo: {}
    })

    player.basicInfo.stamina = player.fightAttri.maxStamina
    player.initAttri.str = opts.str || 0
    player.initAttri.con = opts.con || 0
    player.initAttri.agile = opts.agile || 0
    player.initAttri.wisdom = opts.wisdom || 0
    player.initPlayer()

    //初始的物品
    let sendItems = characterInfo.sendItems
    for (let i = 0; i < sendItems.length; i++) {
      let itemId = sendItems[i][0]
      let itemNum = sendItems[i][1]
      let itemComponent = templateConfig.total[itemId].getItemComponent(itemNum)
      player.bag.addItem(itemComponent)
    }

    //创建新角色信息
    playerDao.newAndSave(player, function(err, res) {
      if (cb) {
        cb(err)
      }
    })
  }
}

module.exports =new CreateNewPlayerHandle()
